package com.moge.anjian.activities;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.LinearLayout;

import androidx.activity.ComponentActivity;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.moge.anjian.R;
import com.moge.anjian.adapter.RecordListAdapter;
import com.moge.anjian.beans.Constant;
import com.moge.anjian.beans.PlayBackBean;
import com.moge.anjian.utils.SpUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class RecordActivity extends ComponentActivity {

    RecyclerView recyclerView;
    RecordListAdapter mAdapter;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);

        recyclerView = findViewById(R.id.recycler_view);

        initRecyclerView();


    }

    private void initRecyclerView() {
        List<PlayBackBean> list = new ArrayList<>();
        String playBackListStr = SpUtils.getString(Constant.KEY_PLAYBACK_LIST);
        if (TextUtils.isEmpty(playBackListStr)) {
            return;
        }
        try {
            JSONArray playBackListArray = new JSONArray(playBackListStr);
            for (int i=0; i<playBackListArray.length(); i++) {
                String name = ((JSONObject)playBackListArray.get(i)).getString("name");
                PlayBackBean playBackBean = new PlayBackBean();
                playBackBean.name = name;
                list.add(playBackBean);
                mAdapter = new RecordListAdapter(this, list);
                recyclerView.setAdapter(mAdapter);
                recyclerView.setLayoutManager(new LinearLayoutManager(RecordActivity.this));
            }
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
    }
}
